/*
 * AlbumCoverArtPanel.java
 *
 * Created on October 7, 2008, 9:37 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.bjj.utils.Resources.LABEL_KEY;
import com.bff.slimserver.music.MusicObjects;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.AlbumList;
import com.bff.slimserver.musicjukebox.ui.components.CoverArtPanel;
import com.bff.slimserver.musicjukebox.ui.components.GenreComboBox;
import com.bff.slimserver.musicjukebox.ui.components.YearComboBox;
import com.bff.bjj.utils.Utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.slimserver.SlimDatabase;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.musicobjects.SlimAlbum;
import org.bff.slimserver.musicobjects.SlimGenre;

/**
 *
 * @author  Bill
 */
public class AlbumBrowserPanel extends CoverArtPanel {

    private List<SlimAlbum> albums;
    private SlimDatabase slimDb;
    private boolean threadRunning;
    private int oldSelectedAlbumIndex;

    /** Creates new form AlbumCoverArtPanel */
    public AlbumBrowserPanel(Collection<SlimAlbum> albums) {
        this.albums = new ArrayList<SlimAlbum>(albums);
        oldSelectedAlbumIndex = -1;
        
        try {
            initComponents();

            slimDb = JukeboxProperties.getInstance().getDatabase();
            albums = new ArrayList<SlimAlbum>(albums);

            loadAlbums();

        } catch (SlimConnectionException ex) {
            Logger.getLogger(AlbumBrowserPanel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            loadProgressBar.setVisible(false);
        }
    }

    private void checkYearIndexes() {
        int startYearIndex = cbYearStart.getSelectedIndex();
        if (startYearIndex == 0) {
            setEndYearEnabled(false);
        } else {
            setEndYearEnabled(true);
        }
        int endYearIndex = cbYearEnd.getSelectedIndex();
        if (endYearIndex < startYearIndex) {
            cbYearEnd.setSelectedIndex(startYearIndex);
        }
    }

    private void filter() {
        setCursor(Utils.getHourglassCursor());
        loadProgressBar.setVisible(true);
        try {
            int startYearIndex = cbYearStart.getSelectedIndex() - 1;
            int endYearIndex = cbYearEnd.getSelectedIndex() - 1;
            SlimGenre genre = ((GenreComboBox) cbGenre).getSelectedGenre();

            try {
                if (startYearIndex <= 0 && genre == null) {
                    albums = new ArrayList<SlimAlbum>(MusicObjects.getAlbums(true));
                } else {
                    List<String> yearList = new ArrayList<String>(((YearComboBox) cbYearStart).getYearList());

                    if (!cbYearEnd.isEnabled()) {
                        albums = new ArrayList<SlimAlbum>(slimDb.listAlbums(null, genre, null));
                    } else {
                        for (int i = startYearIndex; i < endYearIndex + 1; i++) {
                            albums.clear();
                            albums.addAll(slimDb.listAlbums(null, genre, yearList.get(i)));
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            loadAlbums();
        } finally {
            setCursor(Utils.getNormalCursor());
            loadProgressBar.setVisible(false);
        }
    }

    /**
     * 0 based
     * @param page
     */
    private void loadAlbums() {
        getAlbumJList().setAlbums(albums);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bottomPanel = new com.bff.bjj.ui.component.BJJPanel();
        filterPanel = new com.bff.bjj.ui.component.BJJPanel();
        jLabel1 = new javax.swing.JLabel();
        cbYearStart = new YearComboBox();
        jLabel3 = new javax.swing.JLabel();
        cbYearEnd = new YearComboBox();
        jLabel2 = new javax.swing.JLabel();
        cbGenre = new GenreComboBox();
        btnFilter = new com.bff.bjj.ui.component.BJJButton();
        btnReset = new com.bff.bjj.ui.component.BJJButton();
        loadProgressBar = new javax.swing.JProgressBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        albumList = new AlbumList();
        songListTablePanel = new SongListTablePanel();

        setLayout(new java.awt.BorderLayout());

        bottomPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setText(getResource(LABEL_KEY.ALBUM_FILTER_START_YEAR));
        filterPanel.add(jLabel1);

        cbYearStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbYearStartActionPerformed(evt);
            }
        });
        filterPanel.add(cbYearStart);

        jLabel3.setText(getResource(LABEL_KEY.ALBUM_FILTER_END_YEAR));
        jLabel3.setEnabled(false);
        filterPanel.add(jLabel3);

        cbYearEnd.setEnabled(false);
        cbYearEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbYearEndActionPerformed(evt);
            }
        });
        filterPanel.add(cbYearEnd);

        jLabel2.setText(getResource(LABEL_KEY.ALBUM_FILTER_GENRE));
        filterPanel.add(jLabel2);
        filterPanel.add(cbGenre);

        btnFilter.setText(getResource(LABEL_KEY.ALBUM_FILTER_BUTTON_LABEL));
        btnFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFilterActionPerformed(evt);
            }
        });
        filterPanel.add(btnFilter);

        btnReset.setText(getResource(LABEL_KEY.ALBUM_FILTER_RESET_BUTTON_LABEL));
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        filterPanel.add(btnReset);

        bottomPanel.add(filterPanel, java.awt.BorderLayout.CENTER);

        loadProgressBar.setIndeterminate(true);
        loadProgressBar.setString(getResource(LABEL_KEY.ALBUM_FILTER_LOADING_ALBUMS));
        loadProgressBar.setStringPainted(true);
        bottomPanel.add(loadProgressBar, java.awt.BorderLayout.NORTH);

        add(bottomPanel, java.awt.BorderLayout.SOUTH);

        albumList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                albumListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(albumList);

        add(jScrollPane1, java.awt.BorderLayout.WEST);

        songListTablePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getResource(LABEL_KEY.ALBUM_FILTER_BODER_SONGS)));
        add(songListTablePanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void cbYearStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbYearStartActionPerformed
    checkYearIndexes();
}//GEN-LAST:event_cbYearStartActionPerformed

private void btnFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFilterActionPerformed
    filter();
}//GEN-LAST:event_btnFilterActionPerformed

private void cbYearEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbYearEndActionPerformed
    checkYearIndexes();
}//GEN-LAST:event_cbYearEndActionPerformed

private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
    cbGenre.setSelectedIndex(0);
    cbYearEnd.setSelectedIndex(0);
    cbYearStart.setSelectedIndex(0);
    filter();
}//GEN-LAST:event_btnResetActionPerformed

private void albumListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_albumListValueChanged
    if (getAlbumJList().getSelectedIndex() > -1 && getAlbumJList().getSelectedIndex() != oldSelectedAlbumIndex) {
        listSongs();
        oldSelectedAlbumIndex = getAlbumJList().getSelectedIndex();
    }
}//GEN-LAST:event_albumListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList albumList;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JButton btnFilter;
    private javax.swing.JButton btnReset;
    private javax.swing.JComboBox cbGenre;
    private javax.swing.JComboBox cbYearEnd;
    private javax.swing.JComboBox cbYearStart;
    private javax.swing.JPanel filterPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JProgressBar loadProgressBar;
    private javax.swing.JPanel songListTablePanel;
    // End of variables declaration//GEN-END:variables
    private void setEndYearEnabled(boolean b) {
        jLabel3.setEnabled(b);
        cbYearEnd.setEnabled(b);
    }

    private AlbumList getAlbumJList() {
        return (AlbumList) albumList;
    }

    private synchronized void listSongs() {
        while (isThreadRunning()) {
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                Logger.getLogger(ExplorerListPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        new ListSongThread().start();
    }

    private SongListTablePanel getSongListTablePanel() {
        return (SongListTablePanel) songListTablePanel;
    }

    public boolean isThreadRunning() {
        return threadRunning;
    }

    public void setThreadRunning(boolean threadRunning) {
        this.threadRunning = threadRunning;
    }

    private class ListSongThread extends Thread {

        @Override
        public void run() {
            setThreadRunning(true);
            try {
                if (albumList.getSelectedIndex() > -1) {
                    getSongListTablePanel().setAlbum(getAlbumJList().getSelectedAlbum());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            setThreadRunning(false);
        }
    }
}
